<?php
    require_once("./classes/sessao.php");
    require_once("./classes/interfaces.php");
    require_once("./classes/conexao.php");

    session_start();
    if (!isset($_SESSION['ANAWEB']) || $_SESSION['ANAWEB']->RetornaIdioma() == "PT_BR")
    {
        $msg = new Traducao("/home/projetos/anaweb/anaweb-new/linguas/linguas.xml", "pt-br");
    }
    else
    {
        $msg = new Traducao("/home/projetos/anaweb/anaweb-new/linguas/linguas.xml", "en-us");
    }
    if (!isset($_SESSION['ANAWEB']))
    {
        $pagina = new Interfaces();
        $pagina->Header(Nao_Logado);
        $pagina->Body();
        echo Nao_Logado_Msg;
        $pagina->Footer();
    }
    else
    {
        $pagina = new Interfaces();
        $pagina->Header(" ANAWEB ");
        $pagina->Body();
        if (isset($_GET['cat']) && isset($_GET['item']) && isset($_GET['file']))
        {
            if (!isset($_POST['enviar']))
            {
                $arq = base64_decode($_GET['file']);
                $xml = DOMDocument::load($arq);

                $cats = $xml->getelementsbytagname("subject");
                $items = $cats->item($_GET['cat'])->getelementsbytagname("item")->item($_GET['item']);

                $nome = $items->getAttribute("name");

                $desc = $items->getelementsbytagname("description")->item(0)->nodeValue;

                $examples = $items->getelementsbytagname("examples")->item(0);
                $linhas = $examples->getelementsbytagname("example")->item(0);
                $remcom = $examples->getElementsByTagname("remark");
                $remcom = $remcom->item($remcom->length - 1)->nodeValue;
                $linhas = $linhas->getElementsByTagname("sentence");
                $str ="";
                foreach ($linhas as $l)
                {
                    $str .= $l->nodeValue."\n";
                }
                $relcom = $items->getelementsbytagname("related_commands")->item(0);
                $relcoms = $relcom->getElementsByTagname("command");
                $relcom = "";
                for($i=0;$i<$relcoms->length;$i++)
                {
                    $relcom .= (($i != 0)?", ":'').$relcoms->item($i)->nodeValue;
                }
                $try = $items->getElementsByTagname("try_yourself")->item(0);
                $templ = $try->getElementsByTagname("template")->item(0)->nodeValue;
                $remtry = $try->getElementsByTagname("remark")->item(0)->nodeValue;

                $exercises = $items->getElementsByTagname("exercises")->item(0);
                $exercise = $exercises->getElementsByTagname("exercise")->item(0);
                $exdesc = $exercise->getElementsByTagname("description")->item(0)->nodeValue;
                $exhint = $exercise->getElementsByTagname("hint")->item(0)->nodeValue;
                $exsol = $exercise->getElementsByTagname("solution")->item(0)->nodeValue;
                $filehelp = $items->getElementsByTagname("filehelp")->item(0)->nodeValue;
                $help = explode("/",$filehelp);
?>
            <div id="cadastro">
            <form name="formhelp" enctype="multipart/form-data" action="arv_altera.php?file=<?php echo $_GET['file'] ?>&item=<?php echo $_GET['item'] ?>&cat=<?php echo $_GET['cat'] ?>" method="post">
            <fieldset>
            <legend><?php echo $msg->get('Item') ?></legend>
            <p><label for="itname"><?php echo $msg->get('Nome_Item') ?>:</label> <input type="text" name="nome" id="itname" value="<?php echo $nome ?>"></p>
            <p><label for="desc"><?php echo $msg->get('Descricao') ?>:</label> <textarea name="desc" id="desc" cols="40" rows="1"><?php echo $desc ?></textarea></p>
            <p><label for="relcom"><?php echo $msg->get('Comando_Relacionado') ?>:</label> <input type="text" name="relcom" id="relcom" value="<?php echo $relcom ?>"></p>
            </fieldset>
            <fieldset>
            <legend><?php echo $msg->get('Tente_Voce_Mesmo') ?></legend>
            <p><label for="templ"><?php echo $msg->get('Template') ?>:</label> <input type="text" name="templ" id="templ" value="<?php echo $templ ?>"></p>
            <p><label for="remarktry"><?php echo $msg->get('Comentario') ?>:</label> <textarea name="comentarios" id="remarktry" cols="40" rows="1"><?php echo $remtry ?></textarea></p>
            </fieldset>
            <fieldset>
            <legend><?php echo $msg->get('Exemplo') ?></legend>
            <p><label for="com"><?php echo $msg->get('Comandos') ?>:</label> <textarea name="comandos" id="com" cols="40" rows="7" wrap="off"><?php echo $str ?></textarea></p>
            <p><label for="remarkcom"><?php echo $msg->get('Comentario') ?>:</label> <textarea name="comentarioscom" id="remarkcom" cols="40" rows="1"><?php echo $remcom ?></textarea></p>
            </fieldset>
            <fieldset>
            <legend><?php echo $msg->get('Exercicio') ?></legend>
            <p><label for="ex"><?php echo $msg->get('Exercicio') ?>:</label> <textarea name="exercicio" id="ex" cols="40" rows="1"><?php echo $exdesc ?></textarea></p>
            <p><label for="dica"><?php echo $msg->get('Dica') ?>:</label> <textarea name="dica" id="dica" cols="40" rows="1"><?php echo $exhint ?></textarea></p>
            <p><label for="solucao"><?php echo $msg->get('Solucao') ?>:</label> <textarea name="solucao" id="solucao" cols="40" rows="1"><?php echo $exsol ?></textarea></p>
            </fieldset>
            <fieldset>
            <legend><?php echo $msg->get('Arquivo_Ajuda') ?></legend>
<?php
    if ($arq == "./bibliotecas/scilab_valid.xml")
    {
        $topic = -1;
        if (count($help) == 2)
            $topic = $help[0];
?>
        <script type="text/javascript" src="./javascript/helpscilab.php"></script>
        <p><label for="topico"><?php echo $msg->get('Topico') ?>:</label>
        <select name="topico" id="topico" onchange="mudatopico(document.formhelp.topico)">>
            <option value="0"<?php if ($topic == 0) echo " selected" ?>>Programming</option>
            <option value="1"<?php if ($topic == 1) echo " selected" ?>>Graphics Library</option>
            <option value="2"<?php if ($topic == 2) echo " selected" ?>>Elementary Functions</option>
            <option value="3"<?php if ($topic == 3) echo " selected" ?>>Input/Output Functions</option>
            <option value="4"<?php if ($topic == 4) echo " selected" ?>>Handling of functions and libraries</option>
            <option value="5"<?php if ($topic == 5) echo " selected" ?>>Character string manipulations</option>
            <option value="6"<?php if ($topic == 6) echo " selected" ?>>GUI and Dialogs</option>
            <option value="7"<?php if ($topic == 7) echo " selected" ?>>Utilities</option>
            <option value="8"<?php if ($topic == 8) echo " selected" ?>>Linear Algebra</option>
            <option value="9"<?php if ($topic == 9) echo " selected" ?>>Polynomial calculations</option>
            <option value="10"<?php if ($topic == 10) echo " selected" ?>>General System and Control</option>
            <option value="11"<?php if ($topic == 11) echo " selected" ?>>Robust control toolbox</option>
            <option value="12"<?php if ($topic == 12) echo " selected" ?>>Optimization and simulation</option>
            <option value="13"<?php if ($topic == 13) echo " selected" ?>>Signal Processing toolbox</option>
            <option value="14"<?php if ($topic == 14) echo " selected" ?>>Arma modelisation and simulation toolbox</option>
            <option value="15"<?php if ($topic == 15) echo " selected" ?>>Metanet: graph and network toolbox</option>
            <option value="16"<?php if ($topic == 16) echo " selected" ?>>Scicos: Bloc diagram editor and simulator</option>
            <option value="17"<?php if ($topic == 17) echo " selected" ?>>Sound file handling</option>
            <option value="18"<?php if ($topic == 18) echo " selected" ?>>Language or data translations</option>
            <option value="19"<?php if ($topic == 19) echo " selected" ?>>TdCs</option>
            <option value="20"<?php if ($topic == 20) echo " selected" ?>>Statistic basics</option>
            <option value="21"<?php if ($topic == 21) echo " selected" ?>>Cumulative Distribution Functions; Inverses, grand</option>
            <option value="22"<?php if ($topic == 22) echo " selected" ?>>Identification</option>
            <option value="23"<?php if ($topic == 23) echo " selected" ?>>Matlab to Scilab converstion tips</option>
            <option value="24"<?php if ($topic == 24) echo " selected" ?>>Sparse solvers</option>
            <option value="25"<?php if ($topic == 25) echo " selected" ?>>PVM parallel toolbox</option>
            <option value="26"<?php if ($topic == 26) echo " selected" ?>>TCL/Tk interface</option>
            <option value="27"<?php if ($topic == 27) echo " selected" ?>>Java interface</option>
            <option value="nada"<?php if ($topic == -1) echo " selected" ?>>---</option>
        </select></p>
        <p><label for="subtopico"><?php echo $msg->get('Comando') ?>:</label>
        <select name="subtopico">
            <option>--</option>
        </select></p>
        <script type="text/javascript">
            mudatopico(document.formhelp.topico);
        </script>
<?php
    } else {
?>
            <p><label for="arq"><?php echo $msg->get('Comando') ?>:</label> <input type="text" name="ajuda" id="arq" value="<?php echo $filehelp ?>"></p>
<?php
    }
?>
            </fieldset>
            <input type="hidden" name="cat" value="<?php echo $_GET['cat'] ?>">
            <input type="submit" name="enviar" value="<?php echo $msg->get('Enviar') ?>">
            </form>
            </div>
<?php
            }
            elseif (isset($_GET['file']))
            {
                $arq = base64_decode($_GET['file']);
                /*if ($arq == "./bibliotecas/maple_valid.xml")
                {*/
                    require_once("./classes/casxml.php");
                    $xml = new CasXML();
                    $xml->preserveWhiteSpace=false;
                    $xml->load($arq);
                    $xml->formatOutput=true;

                    $name = $_POST['nome'];
                    $folder = $xml->getElementsByTagname("subject")->item($_POST['cat']);
                    $description = $_POST['desc'];
                    $sentence = $_POST['comandos'];
                    $remarksentence = $_POST['comentarioscom'];
                    $remarkexample = $_POST['comentarioscom'];
                    $relatedcommand = $_POST['relcom'];
                    $template = $_POST['templ'];
                    $remarktemplate = $_POST['comentarios'];
                    $exercisedescription = $_POST['exercicio'];
                    $hint = $_POST['dica'];
                    $solution = $_POST['solucao'];
                    $filehelp = @$_POST['ajuda'];
                    /*if (@$_FILES['ajuda']['size'] > 0)
                    {
                        $uploaddir = "./bibliotecas/cas_filehelp/";
                        $userfile = basename($_FILES['ajuda']['name']);
                        $uploadfile = $uploaddir . $userfile;
    
                        if (move_uploaded_file($_FILES['ajuda']['tmp_name'], $uploadfile)) {
                            echo "File is valid, and was successfully uploaded.\n";
                        } else {
                            echo "Possible file upload attack!\n";
                        }
                        $filehelp = $userfile;
                    }*/
                    $folder = $xml->getElementsByTagname("subject")->item($_GET['cat']);
                    $item = $xml->getElementsByTagname("item")->item($_GET['item']);
                    $xml->alterItem( $name, $folder, $item, $description,
                                $sentence, $remarksentence, $remarkexample,
                                $relatedcommand,$template,$remarktemplate,
                                $exercisedescription,$hint,$solution,$filehelp,@$_POST['topico'],@$_POST['subtopico'] );
                    $xml->save($arq);
                /*}
                elseif ($arq != "./bibliotecas/maple_valid.xml")
                {
                    $xml = new DomDocument();
                    $xml->preserveWhiteSpace=false;
                    $xml->load($arq);
                    $xml->formatOutput=true;

                    $cats = $xml->getelementsbytagname("subject");
                    $items = $cats->item($_GET['cat'])->getelementsbytagname("item")->item($_GET['item']);

                    $items->setAttribute("name",$_POST['nome']);

                    $items->getelementsbytagname("description")->item(0)->nodeValue = utf8_encode($desc);

                    $examples = $items->getelementsbytagname("examples")->item(0);
                    $examples->nodeValue='';

                    $comandos = explode("\r\n",$_POST['comandos']);
                    foreach ($comandos as $c)
                    {
                        $example = $xml->createElement("example",$c);
                        $example = $examples->appendChild($example);
                    }
                    $xml->save($arq);
                }*/
?>
            <?php echo $msg->get('Arvore_Alterada_Sucesso'); ?>
            <br>
            <a href="index.php"><?php echo $msg->get('Voltar'); ?></a>
<?php
            }
        }
        elseif (isset($_GET['cat']) && !isset($_GET['item']))
        {
            if (!isset($_POST['enviar']) && isset($_GET['file']))
            {
                $arq = base64_decode($_GET['file']);
                $xml = DOMDocument::load($arq);

                $cats = $xml->getelementsbytagname("subject");
                $items = $cats->item($_GET['cat']);

                $nome = $items->getAttribute("name");
?>
            <div id="cadastro">
            <form name="novoitem" action="arv_altera.php?cat=<?php echo $_GET['cat'] ?>&file=<?php echo $_GET['file'] ?>" method="post">
            <p><label for="itname"><?php echo $msg->get('Nome') ?>:</label> <input type="text" name="nome" id="itname" value="<?php echo $nome ?>"></p>
            <input type="submit" name="enviar" value="<?php echo $msg->get('Enviar') ?>">
            </form>
            </div>
<?php
            }
            elseif (isset($_GET['file']))
            {
                $arq = base64_decode($_GET['file']);
                $xml = new DomDocument();
                $xml->preserveWhiteSpace=false;
                $xml->load($arq);
                $xml->formatOutput=true;

                $cats = $xml->getelementsbytagname("subject");
                $items = $cats->item($_GET['cat']);

                $items->setAttribute("name",$_POST['nome']);

                $xml->save($arq);
?>
            <?php echo $msg->get('Arvore_Alterada_Sucesso'); ?>
            <br>
            <a href="index.php"><?php echo $msg->get('Voltar'); ?></a>
<?php
            }
        }
        $pagina->Footer();
    }
?> 

